/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reconocimientoletras;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author johnny
 */
public class CVentPrinc extends javax.swing.JFrame {

    /**
     * Creates new form CVentPrinc
     */
    Dibujo objDibujo;
    mlp neurona= new mlp();
    public CVentPrinc() {
        initComponents();
         objDibujo= new Dibujo(this.pnlEntradaDibujo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        pnlEntradaDibujo = new javax.swing.JPanel();
        cmdLimpiar = new javax.swing.JButton();
        cmdReconocer = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        dtgModelo = new javax.swing.JTable();
        cmdEntrenar = new javax.swing.JButton();
        cboError = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cboIteraciones = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cboCapas = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnlEntradaDibujo.setBackground(java.awt.Color.white);
        pnlEntradaDibujo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlEntradaDibujo.setPreferredSize(new java.awt.Dimension(140, 140));
        pnlEntradaDibujo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                pnlEntradaDibujoMousePressed(evt);
            }
        });
        pnlEntradaDibujo.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                pnlEntradaDibujoMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout pnlEntradaDibujoLayout = new javax.swing.GroupLayout(pnlEntradaDibujo);
        pnlEntradaDibujo.setLayout(pnlEntradaDibujoLayout);
        pnlEntradaDibujoLayout.setHorizontalGroup(
            pnlEntradaDibujoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 138, Short.MAX_VALUE)
        );
        pnlEntradaDibujoLayout.setVerticalGroup(
            pnlEntradaDibujoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 138, Short.MAX_VALUE)
        );

        cmdLimpiar.setText("Limpiar");
        cmdLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdLimpiarActionPerformed(evt);
            }
        });

        cmdReconocer.setText("Reconocer");
        cmdReconocer.setEnabled(false);
        cmdReconocer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdReconocerActionPerformed(evt);
            }
        });

        jLabel1.setText("Dibuje Aqui la letra");

        dtgModelo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "", "", "", "", "", "", ""
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(dtgModelo);

        cmdEntrenar.setText("Entrenar");
        cmdEntrenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEntrenarActionPerformed(evt);
            }
        });

        cboError.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0.0001", "0.0002", "0.0003", "0.0004", "0.0005", "0.0006", "0.0007", "0.0008", "0.0009", "0.001" }));
        cboError.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboErrorActionPerformed(evt);
            }
        });

        jLabel2.setText("Error");

        jLabel3.setText("Iteraciones");

        cboIteraciones.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "100", "200", "300", "400", "500", "600", "700", "800", "900", "1000", "2000" }));

        jLabel4.setText("Capas");

        cboCapas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "80", "90", "100", "200", "300", "400" }));

        jLabel5.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel5.setText("RECONOCIMIENTO DE PATRONES");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(cmdEntrenar)
                .addGap(20, 20, 20)
                .addComponent(cmdLimpiar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmdReconocer)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(117, 117, 117)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(pnlEntradaDibujo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(38, 38, 38))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(cboError, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(cboIteraciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(cboCapas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 45, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabel2)
                        .addGap(2, 2, 2)
                        .addComponent(cboError, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(jLabel3)
                        .addGap(12, 12, 12)
                        .addComponent(cboIteraciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(jLabel4)
                        .addGap(12, 12, 12)
                        .addComponent(cboCapas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pnlEntradaDibujo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdEntrenar)
                    .addComponent(cmdLimpiar)
                    .addComponent(cmdReconocer))
                .addGap(5, 5, 5))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-481)/2, (screenSize.height-334)/2, 481, 334);
    }// </editor-fold>//GEN-END:initComponents

    private void cmdLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdLimpiarActionPerformed
        // TODO add your handling code here:
        pnlEntradaDibujo.repaint();
        objDibujo.limpiar();
        DefaultTableModel modelo= new DefaultTableModel();
        dtgModelo.setModel(modelo);
    }//GEN-LAST:event_cmdLimpiarActionPerformed

    private void pnlEntradaDibujoMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlEntradaDibujoMousePressed
        // TODO add your handling code here:
        objDibujo.obtenerCordenadas(evt);
    }//GEN-LAST:event_pnlEntradaDibujoMousePressed

    private void pnlEntradaDibujoMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlEntradaDibujoMouseDragged
        // TODO add your handling code here:
        objDibujo.pintar(evt);
    }//GEN-LAST:event_pnlEntradaDibujoMouseDragged

    private void cmdReconocerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdReconocerActionPerformed
        // TODO add your handling code here:
        DefaultTableModel modelo=new DefaultTableModel();
        modelo.setColumnCount(5);
        modelo.setRowCount(7);
        double vectorTest[]=new double[35];
        int matriz[][]=objDibujo.analizaGrafico();
        int pos=0;
        for(int i=0;i<7;i++)
        {
            for(int j=0;j<5;j++)
            {
                System.out.print(matriz[i][j]+",");
                if(matriz[i][j]==1)
                {      
                    modelo.setValueAt('#', i, j);
                    
                }
                vectorTest[pos]=matriz[i][j];
                pos++;
            }
            System.out.println();
        }
        dtgModelo.setModel(modelo);
        JOptionPane.showMessageDialog(this, neurona.provar(vectorTest));
        
    }//GEN-LAST:event_cmdReconocerActionPerformed

    private void cmdEntrenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEntrenarActionPerformed
        // TODO add your handling code here:
        neurona.Entrenar(Double.parseDouble(cboError.getSelectedItem().toString()),Integer.parseInt(cboIteraciones.getSelectedItem().toString()),Integer.parseInt(cboCapas.getSelectedItem().toString()));
        cmdReconocer.setEnabled(true);
        cmdEntrenar.setEnabled(true);
    }//GEN-LAST:event_cmdEntrenarActionPerformed

    private void cboErrorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboErrorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboErrorActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CVentPrinc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CVentPrinc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CVentPrinc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CVentPrinc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CVentPrinc().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cboCapas;
    private javax.swing.JComboBox cboError;
    private javax.swing.JComboBox cboIteraciones;
    private javax.swing.JButton cmdEntrenar;
    private javax.swing.JButton cmdLimpiar;
    private javax.swing.JButton cmdReconocer;
    private javax.swing.JTable dtgModelo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JPanel pnlEntradaDibujo;
    // End of variables declaration//GEN-END:variables
}
